package com.spasvo.dirsyn.runner;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import cn.hutool.setting.Setting;
import com.spasvo.dirsyn.cfg.Config;
import com.spasvo.dirsyn.cfg.FTPConfig;
import com.spasvo.dirsyn.cfg.FolderConfig;
import com.spasvo.dirsyn.listener.SynFileListener;
import com.spasvo.dirsyn.listener.SynFileMonitor;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Map;

@Slf4j
public class Start {

    public void run(String... args) {

        long startTime = System.currentTimeMillis();
        log.info("dirsyn 目录同步 Start...");

        //基础配置解析
        log.info("Config Load...");
        if (args.length != 2 || !"-c".equals(args[0])) {
            log.error("please SpecifyA Profile.");
            System.exit(0);
        }
        File settingFile = new File(args[1]);

        if(!settingFile.exists()){
            log.error("profileDoesNotExist please SpecifyA Profile.");
            System.exit(0);
        }
        Setting setting = new Setting(settingFile.getAbsolutePath());

        Config config = new Config();

        Map<String, String> ftpConfigMap = setting.getMap("ftp");
        ftpConfigMap.forEach((k, v) -> {
            if (k.contains("id")) {
                String tags = k.replace("id", "");
                FTPConfig ftpConfig = new FTPConfig();
                ftpConfig.setId(v);
                ftpConfig.setIp(ftpConfigMap.get("ip" + tags));
                ftpConfig.setName(ftpConfigMap.get("name" + tags));
                ftpConfig.setPassword(ftpConfigMap.get("password" + tags));
                ftpConfig.setFtp(Convert.toBool(ftpConfigMap.get("isFtp" + tags), true));
                ftpConfig.setPort(Convert.toInt(ftpConfigMap.get("port" + tags), 21));
                config.addFtpConfig(v, ftpConfig);
            }
        });
        log.info("FtpConfig Load Success Count {}", config.ftpConfigCount());

        Map<String, String> folderConfigMap = setting.getMap("folder");
        folderConfigMap.forEach((k, v) -> {
            log.info(k+ "  ====:====" + JSONUtil.toJsonStr(v).toString());
            if (k.contains("id")) {
                String tags = k.replace("id", "");
                FolderConfig folderConfig = new FolderConfig();
                folderConfig.setId(v);
                folderConfig.setFtpConfigId(folderConfigMap.get("ftpId" + tags));
                folderConfig.setFtpConfig(config.getFtpConfig(folderConfig.getFtpConfigId()));
                folderConfig.setFtpPath(folderConfigMap.get("ftpPath" + tags));
                folderConfig.setLocalPath(folderConfigMap.get("localPath" + tags));
                folderConfig.setStartSyn(Convert.toBool(folderConfigMap.get("startSyn" + tags), false));

                if (folderConfig.getFtpConfig() == null) {
                    log.warn("FolderConfig ID {} FTPConfig Is Null.", v);
                } else {
                    config.addFolderConfig(v, folderConfig);
                }

            }
        });
        log.info("FolderConfig Load Success Count {}", config.folderConfigCount());

        config.setInterval(Convert.toLong(setting.get("system", "interval"), 3000l));

        log.info("File listening interval {}ms", config.getInterval());

        log.info(JSONUtil.toJsonStr(config.folderConfigList()).toString());
        if (config.folderConfigList().isEmpty()) {
            log.warn("No valid configuration terminates");
            System.exit(0);
        }

        SynFileMonitor synFileMonitor = new SynFileMonitor(config.getInterval());
        //启动文件夹监听
        config.folderConfigList().forEach(item -> {
            SynFileListener synFileListener = new SynFileListener(item);
            synFileMonitor.monitor(item.getLocalPath(), synFileListener);
            log.info("开始监听："+item.getLocalPath());
            log.info("监听："+JSONUtil.toJsonStr(item).toString());
        });


        try {
            synFileMonitor.start();
        } catch (Exception e) {
            log.error("File Listener Error", e);
        }
        log.info("dirsyn 目录监听并同步 Start Success Total Time {}ms", System.currentTimeMillis() - startTime);

    }
}
